<?php


class task_controller
{
    function index()
    {
        header('Content-Type: text/html');
        $rootDir = $_SERVER["DOCUMENT_ROOT"];
        $filename = $rootDir . "/web/dist/index.html";
        if (file_exists($filename)) {
            $file = file_get_contents($filename);
            echo $file;
        } else {
            echo Json::Fail();
        }
    }

    function list($model)
    {
        if (isset($model)) {
            $where = array();
            if (isset($_POST["completed"])) {
                $where['status'] = $_POST["completed"];
            }
            if (isset($_POST["update_time"])) {
                $where['update_time'] = $_POST['update_time'];
            }
            if (isset($_POST["category"])) {
                $where['category'] = $_POST['category'];
            }
            $page = $_POST["page"] ?? 1;
            $size = $_POST["size"] ?? 10;
            $pagination = array("page" => $page, "size" => $size);
            $data = $model->list($where, $pagination);
            echo Json::Success(0, 'Success', $data);
            return;
        }
        echo Json::Fail();
    }

    function add($model)
    {
        if (empty($_POST["task"])) {
            return Json::Fail(1, "数据错误");
        }
        $formData = array("content" => $_POST["task"],);
        if($_POST['category']){
            $formData['category'] = $_POST['category'];
        }
        $result = $model->add($formData);
        echo $result ? Json::Success(0, "Success") : Json::Fail();
        return 0;
    }

    function edit($model)
    {
        if (!empty($_POST["id"])) {
            $formData = [];
            if (isset($_POST["status"])) {
                $formData["status"] = $_POST["status"];
            }
            if(isset($_POST['urgent'])){
                $formData["urgent"] = $_POST["urgent"];
            }
            if (!empty($_POST["del"])) {
                $formData["del"] = $_POST["del"];
            }
            if (!empty($_POST["zhiding"])) {
                $formData["zhiding"] = $_POST["zhiding"];
            }
            if (!empty($_POST["category"])) {
                $formData["category"] = $_POST["category"];
            }
            if (!empty($_POST["task"])) {
                $formData["content"] = $_POST["task"];
            }
            $result = $model->edit($formData, array("id" => $_POST["id"]));
            echo $result ? Json::Success(0, "Success") : Json::Fail();
            return;
        }
        echo Json::Fail(1, "数据错误");
    }

    function chart($model)
    {
        $now = new DateTime();
        $datetime = $_POST['date'] ?? $now->format('Y-m-d');
        $dataKeyType = $_POST['data_type'] ?? 'day';
        $chartKeyType = $_POST['chart_type'] ?? 'week';
        $data = $model->chartView($datetime, $dataKeyType, $chartKeyType);
        echo Json::Success(0, "Success", $data);
    }

    function categories($model){
        echo Json::Success(0, "Success", $model->getCategories());
    }

}